# import uiautomator2 as u2
from time import sleep
from PIL import Image
import numpy as np
import string
import shutil
import base64
import cv2
import re
import os

cilent = '521234c6'
# cilent = '192.168.1.15:55051'
server = '062623987d28'
# server = '192.168.1.5:55051'
os.system("adb devices")
os.system("adb connect %s" %cilent)
# os.system("adb connect %s" %server)
repl = '.!"#$%&\'()*+,-/:;<=>?@[\\]^_`{|}~“”、，。 《》°「‖'

# serverdevi = u2.connect_adb_wifi(server)
input("start>")
os.system("adb -s %s shell ime set com.android.adbkeyboard/.AdbIME" %server)

while True:
    try:
        os.system("adb -s %s shell rm /sdcard/window_dump.xml >nul 2>&1" %server)
        os.remove('window_dump.xml')
        os.remove('1.temp.png')
        os.remove('1.png')
        os.remove('1.box')
    except: print()

    # os.system("adb -s %s shell screencap -p /sdcard/screenshot.png && adb -s %s pull /sdcard/screenshot.png 1.temp.png" %(cilent,cilent)) # under Android 6
    # os.system("adb -s %s exec-out screencap -p > 1.temp.0.png" %cilent) ; open('1.temp.png', 'wb').write(open('1.temp.0.png', 'rb').read().replace(b'\r\n',b'\n')) # over Android 6
    os.system("adb -s %s exec-out screencap -p > 1.temp.png" %cilent)
    Image.open('1.temp.png').crop((50,250,670,1200)).save('1.png', 'PNG')
    # if os.path.exists('1.last.png'):
    #     if os.path.getsize('1.png') == os.path.getsize('1.last.png'):
    #         continue
    # shutil.copy('1.png', '1.last.png')
        
    img = cv2.imread('1.png',0)
    th = 200
    img = (img>th)*255
    cv2.imwrite('1.png', img)

    os.system("tesseract 1.png 1 --dpi 300 -l chi_sim makebox >nul 2>&1")
    contextbox = open('1.box', mode='r', encoding='UTF8').read()
    contextboxnopunc = re.sub(r'[\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~“”、，。《》°](.+)\n', '', re.split(r'(推.+\n荐.+\n)|(出.+\n题.+\n)', contextbox)[0])
    contextnopunc = ''.join(re.split(r' .+\n', contextboxnopunc))

    # open('1.contextboxnopunc.box',mode='w',encoding='UTF8').write(contextboxnopunc)
    # open('1.contextnopunc.txt',mode='w',encoding='UTF8').write(contextnopunc)

    chars = contextnopunc[:15]

    charsb64 = str(base64.b64encode(chars.encode('utf-8')))[1:]
    # print(charsb64)

    # os.system("adb devices")
    # os.system("adb connect 192.168.1.5:55051")

    os.system("adb -s %s shell input tap 300 450" %server)
    os.system("adb -s %s shell am broadcast -a ADB_INPUT_B64 --es msg %s" %(server,charsb64))
    os.system("adb -s %s shell input keyevent 66" %server)
    # os.system("adb -s %s shell ime set com.sohu.inputmethod.sogou.xiaomi/.SogouIME" %server)

    # adb -s 192.168.1.5:55051 exec-out uiautomator dump && adb -s 192.168.1.5:55051 pull /sdcard/window_dump.xml

    #repl = string.punctuation+'“”、，。 《》°'  # !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
    #c = c.translate(str.maketrans('','', repl)) # 查询的题目不应有标点符号
    # re可能是从一行的头和尾开始匹配，而xml只有一行

    anss = []
    retry = 0
    while anss == []:
        if retry >=3: break
        os.system("adb -s %s exec-out uiautomator dump && adb -s %s pull /sdcard/window_dump.xml" %(server,server)) #and sleep(1)
        while not os.path.exists('window_dump.xml'): sleep(0.5)
        anss = re.findall(r'text\=\"答案：(\S+)\"', open('window_dump.xml', mode='r', encoding='UTF8').read())
        retry += 1
    if retry >=3:
        os.system("adb %s shell rm /sdcard/window_dump.xml >nul 2>&1" %server)
        continue
    # anss = re.findall(r'text\=\"答案：(\S+)\"', serverdevi.dump_hierarchy())
    os.system("adb -s %s shell input keyevent KEYCODE_F5" %server)
    print(anss)

    for ans in anss:
        ansnopunc = ans.translate(str.maketrans('','', repl))
        if re.search(ansnopunc, contextnopunc) :
            print(ansnopunc)
            tartemp = 1200 - int(re.findall(ansnopunc.replace('',' ((.+)) 0\n')[8:], contextboxnopunc)[-1][0].split(' ')[1])
            # print(tartemp)
            os.system("adb -s %s shell input tap 500 %s" %(cilent, tartemp))

    sleep(2)
    


